﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenWings.Entities;
using OpenWings.UI.Elements;
using OpenWings.UI.Helpers;

namespace OpenWings.UI.Controls
{
    public partial class GroupsView : UserControl
    {
        public event EventHandler<GroupEventArgs> SelectGroup;
        public event EventHandler<GroupEventArgs> EditGroup;
        public event EventHandler<GroupEventArgs> AddGroup;
        public event EventHandler<GroupEventArgs> DeleteGroup;

        private Group _SelectedGroup;
        List<Group> _Groups;

        public GroupsView()
        {
            InitializeComponent();
        }

        public List<Group> Groups
        {
            get
            {
                return _Groups;
            }
            set
            {
                _Groups = value;
                PopulateTreeView(_Groups);
                SelectedGroup = null;
            }
        }

        void PopulateTreeView(List<Group> groups)
        {
            ListHelper.PopulateTreeView(GroupTreeView, delegate(TreeView view)
            {
                view.Nodes.Clear();
                if (groups != null)
                    foreach (Group OneGroup in groups)
                        view.Nodes.Add(BuildBranch(OneGroup));
            });
        }

        public Group SelectedGroup
        {
            get
            {
                return _SelectedGroup;
            }
            set
            {
                _SelectedGroup = value;

                if (SelectGroup != null)
                    SelectGroup(this, new GroupEventArgs(_SelectedGroup));
            }
        }

        TreeNodeGroup BuildBranch(Group group)
        {
            TreeNodeGroup Node = new TreeNodeGroup(group);
            Node.Text = group.Name;

            if (group.Groups != null)
                foreach (Group OneGroup in group.Groups)
                    Node.Nodes.Add(BuildBranch(OneGroup));

            return Node;
        }

        private void GroupTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNodeGroup NodeGroup = e.Node as TreeNodeGroup;

            if (NodeGroup == null)
                SelectedGroup = null;
            else
                SelectedGroup = NodeGroup.InnerEntity;
        }

        private void GroupTreeView_MouseDown(object sender, MouseEventArgs e)
        {
            TreeNodeGroup NodeGroup = GroupTreeView.HitTest(e.X, e.Y).Node as TreeNodeGroup;

            if (NodeGroup == null)
            {
                GroupTreeView.SelectedNode = null; // AfterSelect event wont be triggered
                SelectedGroup = null;
            }
            else if (e.Button == MouseButtons.Right)
                GroupTreeView.SelectedNode = NodeGroup; // AfterSelect event will be triggered
        }

        private void GroupContextMenu_Opening(object sender, CancelEventArgs e)
        {
            DeleteMenuItem.Text = "&Delete";

            if (SelectedGroup == null)
            {
                DeleteMenuItem.Enabled = false;
                PropertiesMenuItem.Enabled = false;
            }
            else
            {
                DeleteMenuItem.Text = "&Delete " + SelectedGroup.Name;
                DeleteMenuItem.Enabled = true;
                PropertiesMenuItem.Enabled = true;
            }
        }

        private void PropertiesMenuItem_Click(object sender, EventArgs e)
        {
            if (EditGroup != null)
                EditGroup(this, new GroupEventArgs(SelectedGroup));
        }

        private void DeleteMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedGroup == null)
                return;

            if (MessageBox.Show(this, "Are you sure you want to delete " + SelectedGroup.Name + " ?", "Delete group", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (DeleteGroup != null)
                {
                    TreeNodeGroup ParentGroup = (TreeNodeGroup) GroupTreeView.SelectedNode.Parent;

                    if (ParentGroup == null)
                        DeleteGroup(this, new GroupEventArgs(null));
                    else
                        DeleteGroup(this, new GroupEventArgs(ParentGroup.InnerEntity));
                }

                GroupTreeView.SelectedNode.Remove();
            }
        }

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            Group NewGroup = new Group();
            NewGroup.Name = "New group";

            TreeNode NewItem = new TreeNodeGroup(NewGroup);

            if (SelectedGroup == null)
                GroupTreeView.Nodes.Add(NewItem);
            else
                GroupTreeView.SelectedNode.Nodes.Add(NewItem);

            if (AddGroup != null)
                AddGroup(this, new GroupEventArgs(NewGroup));

            GroupTreeView.SelectedNode = NewItem;
        }
    }

    public class GroupEventArgs : EventArgs
    {
        public Group Group;

        public GroupEventArgs(Group group)
        {
            Group = group;
        }
    }
}
